<?

function smarty_function_link( $params, $smarty ) {

	// make sure we have dependent plugins
	$smarty->depend( 'modifier', 'escape' );

	// fetch the url for the request
	$urlHash = is_array($params['url']) ? $params['url'] : Smutty_Utils::strToHash( $params['url'] );
	$url = Smutty_Utils::getUrl( $urlHash );
	$update = $params['update'];
	$handler = $params['handler'];
	$feedback = $params['feedback'];
	$text = $params['text'] ? $params['text'] : ucfirst($urlHash['action']);
	$form = $params['form'];
	$args = Smutty_Utils::strToHash( $params['args'] );
	$class = $params['class'];

	// is it an ajax link?
	if ( $update )
		echo '<a href="javascript:smutty_ajaxCall(' .
				'\'' . $url . '\',' .
				'\'' . $update . '\',' .
				'\'' . $handler . '\',' .
				'null,' .
				'\'' . $feedback . '\'' .
			');">' . smarty_modifier_escape($text) . '</a>';

	// is it a form link?
	elseif( $form ) {
		$argStr = '';
		foreach ( $args as $key => $value )
			if ( $key )
				$argStr .= ",$key:'$value'";
		$argStr = substr( $argStr, 1 );
		$js = 'f=document.getElementById(\'' . $form . '\');' .
			'a={' . $argStr . '};' .
			'for(x in a)f[x].value=a[x];' .
			'f.submit();';
		echo '<a href="javascript:' . $js . '">' . smarty_modifier_escape($text) . '</a>';
	}

	// otherwise assume normal link
	else {
		$queryString = '';
		foreach ( $args as $key => $value )
			$queryString = '&' . urlencode($key) . '=' . urlencode($value);
		$queryString = substr( $queryString, 1 );
		echo '<a href="' . $url .
				( $queryString ? "?$queryString" : '' ) .
			'"' .
				( $class ? ' class="' . $class . '" ' : '' ) .
			'>' .
			smarty_modifier_escape($text) . '</a>';
	}

}

?>